package com.jxc.jxc01.grc.dao.impl;

import com.jxc.jxc01.grc.dao.IManifestDao;
import com.jxc.jxc01.grc.entity.JDBCConn;
import com.jxc.jxc01.grc.entity.Manifest;
import com.sun.org.apache.bcel.internal.generic.NEW;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ManifestDaoImpl implements IManifestDao {
    @Override
    public boolean addOneOfInBill(Manifest manifest) {
        Connection grc = null;
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            grc = DriverManager.getConnection("jdbc:mysql://172.30.2.4:3306/jxc",
//                    "grc", "123456");
            String sql = "insert into lnbound_checklist(numbering,warehouse,commodity,quantity,specification,price,total," +
                    "depositor,whethe,quality,Inbound_time,creation_time,remark) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement ps=JDBCConn.connect(sql);
            ps.setInt(1, manifest.getNumbering());
            ps.setString(2, manifest.getWarehouse());
            ps.setString(3, manifest.getCommodity());
            ps.setString(4, manifest.getQuantity());
            ps.setString(5, manifest.getSpecification());
            ps.setDouble(6, manifest.getPrice());
            ps.setDouble(7, manifest.getTotal());
            ps.setString(8, manifest.getDepositor());
            ps.setString(9, manifest.getWhethe());
            ps.setString(10, manifest.getQuality());
            ps.setString(11, manifest.getInbound_time());
            ps.setString(12, manifest.getCreation_time());
            ps.setString(13, manifest.getRemark());
            int i = ps.executeUpdate();
            if (i>0){
                return true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public boolean deleteOneOfInBill(int id) {
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            grc = DriverManager.getConnection("jdbc:mysql://172.30.2.4:3306/jxc",
//                    "grc", "123456");
            String sql = "delete from lnbound_checklist where id=?";
//            PreparedStatement ps = grc.prepareStatement(sql);
            PreparedStatement ps=JDBCConn.connect(sql);
            ps.setInt(1, id);
            int i = ps.executeUpdate();
            if (i > 0) {
                return true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public boolean updateCheckStatusById(Manifest manifest) {
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            grc = DriverManager.getConnection("jdbc:mysql://172.30.2.4:3306/jxc",
//                    "grc", "123456");
//            JDBCConn jdbcConn = new JDBCConn();
            String sql = "update lnbound_checklist set " +
                    " warehouse=?,commodity= ? ,quantity= ?,specification=?," +
                    "price=?,total=?,whethe=?," +
                    "quality=?,Inbound_time=?,remark=? where id=?";
            PreparedStatement ps=JDBCConn.connect(sql);
            ps.setString(1, manifest.getWarehouse());
            ps.setString(2, manifest.getCommodity());
            ps.setString(3, manifest.getQuantity());
            ps.setString(4, manifest.getSpecification());
            ps.setDouble(5, manifest.getPrice());
            ps.setDouble(6, manifest.getTotal());
            ps.setString(7, manifest.getWhethe());
            ps.setString(8, manifest.getQuality());
            ps.setString(9,manifest.getCreation_time());
            ps.setString(10, manifest.getRemark());
            ps.setInt(11, manifest.getId());
            int i = ps.executeUpdate();
            if (i > 0) {
                return true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    @Override
    public List<Manifest> queryOneOfInBill() {
        List<Manifest> manifestList = new ArrayList<>();
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            grc = DriverManager.getConnection("jdbc:mysql://172.30.2.4:3306/jxc",
//                    "grc", "123456");
            String sql = "select * from lnbound_checklist";
            PreparedStatement ps=JDBCConn.connect(sql);
//            ps = jdbcConn.connect(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Manifest manifest = new Manifest();
                manifest.setId(rs.getInt("id"));
                manifest.setNumbering(rs.getInt("numbering"));
                manifest.setWarehouse(rs.getString("warehouse"));
                manifest.setCommodity(rs.getString("commodity"));
                manifest.setQuantity(rs.getString("quantity"));
                manifest.setSpecification(rs.getString("specification"));
                manifest.setPrice(rs.getDouble("price"));
                manifest.setTotal(rs.getDouble("total"));
                manifest.setDepositor(rs.getString("depositor"));
                manifest.setWhethe(rs.getString("whethe"));
                manifest.setQuality(rs.getString("quality"));
                manifest.setInbound_time(rs.getString("Inbound_time"));
                manifest.setCreation_time(rs.getString("creation_time"));
                manifest.setRemark(rs.getString("remark"));
                manifestList.add(manifest);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return manifestList;
    }

    @Override
    public int queryOneOfInBillTotalCount() {
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            grc = DriverManager.getConnection("jdbc:mysql://172.30.2.4:3306/jxc",
//                    "grc", "123456");
            String sql = "select count(*) from lnbound_checklist";
            PreparedStatement ps=JDBCConn.connect(sql);

            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                int anInt = rs.getInt(1);
                return anInt;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Manifest> queryById(int numbering) {
        List<Manifest> manifestList = new ArrayList<>();
        try {
//            JDBCConn jdbcConn = new JDBCConn();
//            Class.forName("com.mysql.jdbc.Driver");
//            grc = DriverManager.getConnection("jdbc:mysql://172.30.2.4:3306/jxc",
//                    "grc", "123456");
            String sql = "select * from lnbound_checklist where numbering=?";
            PreparedStatement ps=JDBCConn.connect(sql);
            ps.setInt(1, numbering);
            ResultSet  rs = ps.executeQuery();
            while (rs.next()) {
                Manifest manifest = new Manifest();
                manifest.setId(rs.getInt("id"));
                manifest.setNumbering(rs.getInt("numbering"));
                manifest.setWarehouse(rs.getString("warehouse"));
                manifest.setCommodity(rs.getString("commodity"));
                manifest.setQuantity(rs.getString("quantity"));
                manifest.setSpecification(rs.getString("specification"));
                manifest.setPrice(rs.getDouble("price"));
                manifest.setTotal(rs.getDouble("total"));
                manifest.setDepositor(rs.getString("depositor"));
                manifest.setWhethe(rs.getString("whethe"));
                manifest.setQuality(rs.getString("quality"));
                manifest.setInbound_time(rs.getString("Inbound_time"));
                manifest.setCreation_time(rs.getString("creation_time"));
                manifest.setRemark(rs.getString("remark"));
                manifestList.add(manifest);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return manifestList;
    }
}
